package com.mahees.portfolio.service

import com.mahees.portfolio.domain.*;

import grails.test.*
import grails.validation.ValidationException;

class ProjectServiceTests extends GrailsUnitTestCase {

	ProjectService projectService
	SoftwareProject healthPlus
	List<DevelopmentTool> developmentTools
	def hasManyOptions

	protected void setUp() {
		super.setUp()

		projectService = new ProjectService()

		mockDomain(SoftwareProject);
		mockDomain(DevelopmentTool);

		healthPlus = new SoftwareProject(
				name : 'Health Plus Systems',
				url : 'https://healthplussystems.com',
				description : 'project i worked on',
				projectType : SoftwareProjectTypeEnum.WEB
				)

		developmentTools = [
			new DevelopmentTool(name : 'Eclipse',
			description : 'Built in Java, many plugins available',
			url : 'http://www.eclipse.org/',
			toolType : DevelopmentToolTypeEnum.IDE
			).save(failOnError : true),
			new DevelopmentTool(name : 'SpringSource Tool Suite',
			url : 'http://www.springsource.com/developer/sts',
			toolType : DevelopmentToolTypeEnum.IDE
			).save(failOnError : true)
		]
		
		hasManyOptions = [:]
	}

	protected void tearDown() {
		super.tearDown()
	}
	
	void testSaveProject_SavesSuccessfullyWithTwoDevTool() {
		hasManyOptions.developmentTools = [ developmentTools[0], developmentTools[1]];
		
		projectService.saveSoftwareProject(healthPlus, hasManyOptions)

		assert healthPlus
		assert healthPlus.developmentTools.size() == 2
	}

	void testSaveProject_SavesSuccessfullyWithOneDevTool() {
		hasManyOptions.developmentTools = [ developmentTools[0] ];
		
		projectService.saveSoftwareProject(healthPlus, hasManyOptions)

		assert healthPlus
		assert healthPlus.developmentTools.size() == 1
	}


	void testSaveProject_SavesSuccessfully() {
		projectService.saveSoftwareProject(healthPlus, hasManyOptions)

		assert healthPlus
		assert !healthPlus.developmentTools?.size()
	}

	void testSaveProject_ThrowsValidationExecption() {
		healthPlus.name = '';

		shouldFail(ValidationException) {
			projectService.saveSoftwareProject(healthPlus, hasManyOptions)
		}
	}
}
